#!/usr/bin/bash

# Constants
readonly SCOPE=`basename $0`
readonly UDEV_FILE='/etc/udev/rules.d/70-persistent-net.rules'
readonly SUCCESS=0
readonly FAILURE=1

# Variables
STORAGE_PORT='<%=@storage_parent%>'
ISER_NAME='<%=@iser_interface_name%>'
FATHER_FIRST_VF="/sys/class/net/$STORAGE_PORT/device/virtfn0"

# This functions print logs to /var/log/messages
function logger_print () {
   priority=$1
   msg=$2
   logger -t $SCOPE "$priority: $msg"
}

# Check that a first probe VF exists
if [ ! -d $FATHER_FIRST_VF ]; then
    logger_print  err  "Did not find probed ports of ${STORAGE_PORT}, skipping rename."
    exit $FAILURE
fi

DEVICES='/sys/class/net/*/device'
SON_BUS=`basename $(readlink /sys/class/net/$STORAGE_PORT/device/virtfn0)`
STORAGE_PORT_NUMBER=`cat /sys/class/net/$STORAGE_PORT/dev_id`

# Find the probe VF port that fits the storage ports number and BUS
for dev in $DEVICES; do
  # Check for correct bus
  CANDIDATE_BUS=`readlink -nq $dev`;
  if [[  $CANDIDATE_BUS != *$SON_BUS* ]]; then
    continue;
  fi

  # Check for correct dev_id
  CANDIDATE_DIRNAME=`dirname $dev`
  PORT_NUMBER=`cat $CANDIDATE_DIRNAME/dev_id`
  if [ $PORT_NUMBER = $STORAGE_PORT_NUMBER ]; then
      PROBED_DIRNAME=`dirname $dev`
      PROBED_PORT_NAME=`basename $PROBED_DIRNAME`
  fi
done

# Verify that we find the appropriate virtual port
if [ -z "$PROBED_PORT_NAME" ]; then
    logger_print  err "Did not find $STORAGE_PORT_NUMBER probed ports of $STORAGE_PORT, exiting."
    exit $FAILURE
fi

# Verify that udev file exists
if [ ! -r "$UDEV_FILE" ]; then
    logger_print  err "Did not find $UDEV_FILE to rename iser port."
    exit $FAILURE
fi

# Persistantly rename the matched probed port
if [ $PROBED_PORT_NAME != $ISER_NAME ]; then
  #Prepare line for udev
  UDEV_LINE="SUBSYSTEM==\"net\", ACTION==\"add\", "
  UDEV_LINE+="ATTR{dev_id}==\"$STORAGE_PORT_NUMBER\", KERNELS==\"$SON_BUS\", "
  UDEV_LINE+="ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"$ISER_NAME\""

  # Change/add line in udev file
  grep $PROBED_PORT_NAME $UDEV_FILE > /dev/null 2>&1
  if [ $? -eq $SUCCESS  ]; then
    OLD_LINE_NUMBER=`grep -n $PROBED_PORT_NAME $UDEV_FILE | cut -d : -f 1`
    eval "sed '"$OLD_LINE_NUMBER"d' -i $UDEV_FILE"
  fi
  echo $UDEV_LINE >> $UDEV_FILE

  # Restart Mellanox drivers
  modprobe -r mlx4_en && modprobe mlx4_en
  if [ $? -ne $SUCCESS ]; then
    logger_print  err "Mellanox drivers restart failed."
    exit $FAILURE
  fi

  logger_print info "Changed probed port name from $PROBED_PORT_NAME to $ISER_NAME."
else
  logger_print info "Probed port name is configured properly to $ISER_NAME."
fi

exit $SUCCESS
